﻿Imports System.IO

Public Class Form1
    Public sources As New List(Of String)
    Public fileType As String
    Public searchText As String
    Public results As New List(Of String)
    'Public txtMime() As String = New String() {".nfo", ".asp", ".css", ".js", ".acgi", ".htm", ".html", ".htmls", ".htx", ".shtml", ".js", ".mcf", ".pas", ".c", ".c++", ".cc", ".com", ".conf", ".cxx", ".def", ".f", ".f90", ".for", ".g", ".h", ".hh", ".idc", ".jav", ".java", ".list", ".log", ".lst", ".m", ".mar", ".pl", ".sdml", ".text", ".txt", ".rt", ".rtf", ".rtx", ".wsc", ".sgm", ".sgml", ".tsv", ".uni", ".unis", ".uri", ".uris", ".abc", ".flx", ".rt", ".wml", ".wmls", ".htt", ".asm", ".s", ".aip", ".c", ".cc", ".cpp", ".htc", ".f", ".f77", ".f90", ".for", ".h", ".hh", ".jav", ".java", ".lsx", ".m", ".xml", ".p", ".hlb", ".csh", ".el", ".scm", ".ksh", ".lsp", ".pl", ".pm", ".py", ".rexx", ".scm", ".sh", ".tcl", ".tcsh", ".zsh", ".shtml", ".ssi", ".etx", ".sgm", ".sgml", ".spc", ".talk", ".uil", ".uu", ".uue", ".vcs"}
    Dim li As ListViewItem

    Dim fileList As New List(Of String)


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


    End Sub

#Region "Find"
    Sub searchDIR()
        Dim nameOfDirectory As String = FolderBrowserDialog1.SelectedPath
        Dim myDirectory As DirectoryInfo
        myDirectory = New DirectoryInfo(nameOfDirectory)
        If CheckBox1.Checked = True Then
            WorkWithDirectory(myDirectory)
        ElseIf CheckBox1.Checked = False Then
            WorkWithFilesInDir(myDirectory)
        End If

    End Sub

    Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
        Dim nextDir As DirectoryInfo
        WorkWithFilesInDir(aDir)
        Try
            For Each nextDir In aDir.GetDirectories
                WorkWithDirectory(nextDir)
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try

    End Sub

    Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
        Dim aFile As FileInfo
        Try
            For Each aFile In aDir.GetFiles()
                If TextBox2.Text <> "" Then
                    If InStr(aFile.Name.ToLower, TextBox2.Text.ToLower) Then
                        If TextBox1.Text <> "" Then
                            'If txtMime.Contains(aFile.Extension.ToLower) Then
                            'MsgBox(aFile.Name)
                            findLine(aFile.FullName)
                            'Else
                            'MsgBox("Can only read Text Files")
                            'End If
                        Else
                            li = ListView1.Items.Add(aFile.Name)
                            li.SubItems.Add(aFile.DirectoryName)
                        End If
                    End If
                Else
                    'If txtMime.Contains(aFile.Extension.ToLower) Then
                    'MsgBox(aFile.Name)
                    findLine(aFile.FullName)
                    'End If

                End If
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try

    End Sub
#End Region

    Sub findLine(File As String)
        Dim aFile As New FileInfo(File)
        Dim ofile As StreamReader
        Dim Line As String
        Try
            ofile = New StreamReader(aFile.OpenRead)
            While Not ofile.EndOfStream
                Line = ofile.ReadLine()
                If InStr(Line.ToLower, searchText.ToLower) Then
                    Try

                        li = ListView1.Items.Add(aFile.Name)
                        li.SubItems.Add(aFile.DirectoryName)
                        li.SubItems.Add(Line)

                    Catch ex As Exception
                        'file already added
                    End Try
                End If
            End While
            ofile.Close()

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try


    End Sub


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ListView1.Items.Clear()

        If FolderBrowserDialog1.SelectedPath = "" Then
            MsgBox("please select a directory")
        ElseIf TextBox1.Text = "" And TextBox2.Text = "" Then
            MsgBox("please enter a search query")
        Else
            searchText = TextBox1.Text
            'searchDIR()
            BackgroundWorker1.RunWorkerAsync()

        End If
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        FolderBrowserDialog1.ShowDialog()

    End Sub




    Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.DoubleClick
        Process.Start(ListView1.SelectedItems(0).SubItems(1).Text & "\" & ListView1.SelectedItems(0).SubItems(0).Text)
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Me.Close()

    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SaveFileDialog1.ShowDialog()

    End Sub

    Private Sub SaveFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim sfile As StreamWriter
        Try
            sfile = New StreamWriter(SaveFileDialog1.FileName)
            For Each li As ListViewItem In ListView1.Items
                sfile.WriteLine(Chr(34) & li.SubItems(1).Text & Chr(34) & "," & Chr(34) & li.SubItems(0).Text & Chr(34) & "," & Chr(34) & li.SubItems(2).Text & Chr(34) & ",")
            Next
            sfile.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim rootDirectory As New DirectoryInfo(FolderBrowserDialog1.SelectedPath)
        Dim fileList As New List(Of String)
        fileList = DirectorySearch(rootDirectory, CheckBox1.Checked, 1)
        For Each File In fileList
            findLine(File)
            BackgroundWorker1.ReportProgress(fileList.FindIndex(File))

        Next
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ListView1.Items.Add(File)

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        For Each File In fileList
            ListView1.Items.Add(File)
        Next
    End Sub

    Private Sub SearchFile(File As FileInfo)
        If TextBox2.Text <> "" Then
            If InStr(File.Name.ToLower, TextBox2.Text.ToLower) Then
                If TextBox1.Text <> "" Then
                    'If txtMime.Contains(aFile.Extension.ToLower) Then
                    'MsgBox(aFile.Name)
                    findLine(File.FullName)
                    'Else
                    'MsgBox("Can only read Text Files")
                    'End If
                Else
                    li = ListView1.Items.Add(File.Name)
                    li.SubItems.Add(File.DirectoryName)
                End If
            End If
        Else
            'If txtMime.Contains(aFile.Extension.ToLower) Then
            'MsgBox(aFile.Name)
            findLine(File.FullName)
            'End If

        End If


    End Sub

    Private Function DirectorySearch(Directory As DirectoryInfo, recursive As Boolean, dirLevel As Integer) As List(Of String)
        Dim dirIter As IO.DirectoryInfo
        Dim fileInfo As IO.FileInfo
        If dirLevel = 1 Then fileList.Clear()

        ' Read first the subdirectories
        For Each dirIter In Directory.GetDirectories()
            If recursive Then
                ' Increment the directory level
                dirLevel = dirLevel + 1

                ' Call the procedure for each subdirectories
                DirectorySearch(dirIter, recursive, dirLevel)
            End If
        Next

        ' Then read the files
        For Each fileInfo In Directory.GetFiles()
            fileList.Add(fileInfo.FullName)
        Next
        Return fileList
    End Function

End Class
